package com.sita.bluetactivity.LoginAndRegister;

import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

import com.sita.bluetactivity.Base.BaseActivity;
import com.sita.bluetactivity.Base.ToastUtils;
import com.sita.bluetactivity.R;
import com.sita.bluetactivity.http.RestClient;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import retrofit.Callback;
import retrofit.RetrofitError;
import retrofit.client.Response;

/**
 * Created by Jason on 2016/10/18.
 */

public class RegisterActivity extends BaseActivity implements View.OnClickListener,View.OnFocusChangeListener{
    private EditText registerPhone,registerMsg,registerPass,register_confirmPass;
    private Button getMsgBtn,registerBt;
    private ProgressDialog dialog;
    private RelativeLayout jumpLogin;
    private LinearLayout registerPhoneLayout,registerPassLayout,registerMsgLayout,registerConfirmPassLayout;
    @Override
    protected void initData() {
        getMsgBtn.setOnClickListener(this);
        registerBt.setOnClickListener(this);
        jumpLogin.setOnClickListener(this);
    }

    @Override
    protected void initView() {
        registerPhone = bindView(R.id.register_phone);
        registerPhone.setOnFocusChangeListener(this);
        registerMsg = bindView(R.id.register_msg);
        registerMsg.setOnFocusChangeListener(this);
        registerPass = bindView(R.id.register_pass);
        registerPass.setOnFocusChangeListener(this);
        register_confirmPass = bindView(R.id.register_confirm_pass);
        register_confirmPass.setOnFocusChangeListener(this);
        getMsgBtn = bindView(R.id.getMsg_btn);
        registerBt = bindView(R.id.register_bt);
        jumpLogin = bindView(R.id.jump_registers);
        registerPhoneLayout = bindView(R.id.register_phone_layout);
        registerPassLayout = bindView(R.id.register_pass_layout);
        registerMsgLayout = bindView(R.id.register_msg_layout);
        registerConfirmPassLayout = bindView(R.id.register_confirm_pass_layout);


    }
    @Override
    public void onFocusChange(View v, boolean hasFocus) {
        switch (v.getId()){
            case R.id.register_phone:
                if (hasFocus){
                    registerPhoneLayout.setBackground(getDrawable(R.drawable.frame_style));
                }
                else {
                    registerPhoneLayout.setBackground(getDrawable(R.drawable.frame_nofocus_style));
                }
                break;
            case R.id.register_msg:
                if (hasFocus){
                    registerMsgLayout.setBackground(getDrawable(R.drawable.frame_style));
                }
                else {
                    registerMsgLayout.setBackground(getDrawable(R.drawable.frame_nofocus_style));
                }
                break;
            case R.id.register_pass:
                if (hasFocus){
                    registerPassLayout.setBackground(getDrawable(R.drawable.frame_style));
                }
                else {
                    registerPassLayout.setBackground(getDrawable(R.drawable.frame_nofocus_style));
                }
                break;
            case R.id.register_confirm_pass:
                if (hasFocus){
                    registerConfirmPassLayout.setBackground(getDrawable(R.drawable.frame_style));
                }
                else {
                    registerConfirmPassLayout.setBackground(getDrawable(R.drawable.frame_nofocus_style));
                }
                break;
        }
    }


    @Override
    protected int getId() {
        return R.layout.activity_register;
    }
    public static boolean isMobileNO(String mobiles) {
    /*
    移动：134、135、136、137、138、139、150、151、157(TD)、158、159、187、188
    联通：130、131、132、152、155、156、185、186
    电信：133、153、180、189、（1349卫通）
    总结起来就是第一位必定为1，第二位必定为3或5或8，其他位置的可以为0-9
    */
        String telRegex = "[1][358]\\d{9}";//"[1]"代表第1位为数字1，"[358]"代表第二位可以为3、5、8中的一个，"\\d{9}"代表后面是可以是0～9的数字，有9位。
        if (TextUtils.isEmpty(mobiles)) return false;
        else return mobiles.matches(telRegex);
    }
    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.getMsg_btn:
                //获取验证码
                if (registerPhone.getText().toString().length()!=0&&isPhoneNumberValid(registerPhone.getText().toString())){
                    String phone = registerPhone.getText().toString();
                    RestClient.getRestService().getMsg(phone, 1, new Callback<MsgBackBean>() {
                        @Override
                        public void success(MsgBackBean msgBackBean, Response response) {
                            if (msgBackBean.getErrorCode().equals("0")){
                                if (msgBackBean.getData().getErrmsg().equals("OK")){
                                    Log.e("Register","短信获取成功");
                                }
                            }
                        }

                        @Override
                        public void failure(RetrofitError error) {

                        }
                    });
                }
                else {
                    ToastUtils.show("确认手机号输入正确",3000);
                }


                break;
            case R.id.register_bt:
                dialog = ProgressDialog.show(this,null,"注册中...");
                //注册
                registerMsg();
                break;
            case R.id.jump_registers:
                LoginActivity.jumpLoginIntent(this);
                finish();
                break;
        }
    }

    private void registerMsg(){
        String phone = registerPhone.getText().toString();
        String pass  =registerPass.getText().toString();
        String comfrim = register_confirmPass.getText().toString();
        String msg = registerMsg.getText().toString();
        if (phone.length()==0){
            dialog.dismiss();
            ToastUtils.show("手机号为空",2000);
        }
        else if (pass.equals(comfrim)&&msg.length()!=0){
            RegisterRequest registerRequest = new RegisterRequest();
            Log.e("sss",phone+" s  "+pass+"  s  "+msg+"  ssss  ");
            registerRequest.mobile =phone;
            registerRequest.password = pass;
            registerRequest.userType = 1;
            registerRequest.smscode = msg;
            RestClient.getRestService().register(registerRequest, new Callback<RegisterBackBean>() {
                @Override
                public void success(RegisterBackBean registerBackBean, Response response) {
                    dialog.dismiss();
                    switch (registerBackBean.getErrorCode()){
                        case "0":
                            LoginActivity.jumpLoginIntent(RegisterActivity.this);
                            finish();
                            break;
                    }

                }

                @Override
                public void failure(RetrofitError error) {
                    dialog.dismiss();
                    ToastUtils.show("手机号已注册,或确保验证码输入正确",2000);
                }
            });
        }
        else {
            dialog.dismiss();
            ToastUtils.show("确认密码,和验证码输入正确",2000);
        }

    }
    public static void jumpRegister(Context context){
        Intent intent = new Intent(context,RegisterActivity.class);
        context.startActivity(intent);
    }
    public static boolean isPhoneNumberValid(String phoneNumber) {
        boolean isValid = false;
	   /*
	    * 可接受的电话格式有：
	    */
        String expression = "^\\(?(\\d{3})\\)?[- ]?(\\d{3})[- ]?(\\d{5})$";
	   /*
	    * 可接受的电话格式有：
	    */
        String expression2 = "^\\(?(\\d{3})\\)?[- ]?(\\d{4})[- ]?(\\d{4})$";
        CharSequence inputStr = phoneNumber;
        Pattern pattern = Pattern.compile(expression);
        Matcher matcher = pattern.matcher(inputStr);

        Pattern pattern2 = Pattern.compile(expression2);
        Matcher matcher2 = pattern2.matcher(inputStr);
        if(matcher.matches() || matcher2.matches()) {
            isValid = true;
        }
        return isValid;
    }


}
